package io.gitee.zhangbinhub.admin.controller.inner

import io.gitee.zhangbinhub.acp.boot.exceptions.WebException
import io.gitee.zhangbinhub.acp.boot.log.LogAdapter
import io.gitee.zhangbinhub.admin.api.CommonPath
import io.gitee.zhangbinhub.admin.api.ServerApi
import io.gitee.zhangbinhub.admin.base.BaseController
import io.gitee.zhangbinhub.admin.entity.Application
import io.gitee.zhangbinhub.admin.service.ApplicationService
import io.gitee.zhangbinhub.admin.tools.TokenTools
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

/**
 * @since JDK 11
 */
@Validated
@RestController
@RequestMapping(CommonPath.innerBasePath)
@Tag(name = "应用信息（内部接口）")
class InnerApplicationController @Autowired
constructor(
    logAdapter: LogAdapter,
    private val tokenTools: TokenTools,
    private val applicationService: ApplicationService
) : BaseController(logAdapter) {

    @Operation(summary = "获取应用信息", description = "根据token查询应用详细信息")
    @GetMapping(value = [ServerApi.appInfo], produces = [MediaType.APPLICATION_JSON_VALUE])
    @Throws(WebException::class)
    fun appInfo(): ResponseEntity<Application> =
        applicationService.getApp(tokenTools.getAccessTokenModel()?.clientId ?: "")?.let {
            ResponseEntity.ok(it)
        } ?: throw WebException("找不到应用信息")

}
